package com.goods.service.impl;


import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    ProductCategoryMapper productStockMapper;


    @Override
    public List<ProductCategory> getList() {
        return productStockMapper.selectAll();
    }

    @Override
    public List<ProductCategory> categoryTree() {
        return productStockMapper.selectAll();
    }

    @Override
    public int add(ProductCategoryVO productCategoryVO) {
        Long pid = productCategoryVO.getPid();
        ProductCategory productCategory = new ProductCategory();
        productCategory.setName(productCategoryVO.getName());
        productCategory.setRemark(productCategoryVO.getRemark());
        productCategory.setSort(productCategoryVO.getSort());
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());

        if (pid == null){
            //一级
            productCategory.setPid(new Long(0));
            int i = productStockMapper.insert(productCategory);
            return i;
        }else {
            productCategory.setPid(productCategoryVO.getPid());
            //父级
            int i = productStockMapper.insert(productCategory);
            return i;
        }
    }

    @Override
    public ProductCategory edit(Long id) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setId(id);
        ProductCategory productCategory1 = productStockMapper.selectOne(productCategory);
        return productCategory1;
    }

    @Override
    public int update(Long id, ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setId(id);
        productCategory.setName(productCategoryVO.getName());
        productCategory.setRemark(productCategoryVO.getRemark());
        productCategory.setSort(productCategoryVO.getSort());
        productCategory.setCreateTime(productCategoryVO.getCreateTime());
        productCategory.setModifiedTime(new Date());
        productCategory.setPid(productCategoryVO.getPid());
        int i = productStockMapper.updateByPrimaryKey(productCategory);
        return i;
    }

    @Override
    public int delete(Long id) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setId(id);
        int i = 0;
        /*ProductCategory productCategorySon = productStockMapper.selectSon(productCategory.getId());

        if (productCategorySon != null){
            ProductCategory productCategorySonSon = productStockMapper.selectSon(productCategorySon.getId());
            if (productCategorySonSon != null){
                productStockMapper.delete(productCategorySonSon);
            }
            productStockMapper.delete(productCategorySon);
        }*/
        i = productStockMapper.delete(productCategory);

        return i;
    }
}
